using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Xml;
using System.Web;
using System.Web.Services;
using Dimok.Aspx.Manager.Lib;
using Dimok.Exceptions;
using Data;

namespace Dimok.Aspx.Manager.Common.WS{
	public class Dealers : System.Web.Services.WebService{
		private UOX uox = new UOX();
		public Dealers()
		{
			//CODEGEN: This call is required by the ASP.NET Web Services Designer
			InitializeComponent();
		}

		#region Component Designer generated code
		
		//Required by the Web Services Designer 
		private IContainer components = null;
				
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() {
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing ) {
			if(disposing && components != null) {
				components.Dispose();
			}
			base.Dispose(disposing);		
		}
		
		#endregion

		public const string DEALERS_TABLE = "t_Dealer";

		[WebMethod]
		public string List(string filter) {
			try{
				if( filter == "" )filter = "1=0";
				filter = filter.Replace('*','%');
				return SQL.ADOToString("SELECT * FROM "+DEALERS_TABLE+" WHERE "+SQL.CheckInjects(filter));
			}catch(Exception exc){ throw new ClientException(exc); }
		}
		[WebMethod]
		public void Update(int dealerID,string fieldName,object fieldValue){
			try{
				SQL.UpdateField(uox.db,"SELECT * FROM "+DEALERS_TABLE+" WHERE ID="+dealerID,fieldName,fieldValue);
			}catch(Exception exc){ throw new ClientException(exc); }
		}
		[WebMethod]
		public void Delete(int dealerID){
			try{
			SQL.ExecuteNonQueryEx(uox.db,"DELETE FROM "+DEALERS_TABLE+" WHERE ID="+dealerID);
			}catch(Exception exc){ throw new ClientException(exc); }
		}
		[WebMethod]
		public int Add(XmlElement xmlDataSet){
			try{
				using(SqlConnection conn = new SqlConnection(uox.db)){
					conn.Open();
					SqlTransaction tran = conn.BeginTransaction();
					DataTable dt = SQL.LoadDataTable(xmlDataSet);
					int dealerID = (int)SQL.ExecSP(tran,"s_Dealer_Add",dt.Rows[0]["Name"]);
					SQL.UpdateDataTable(dt,conn,tran,"SELECT * FROM t_Dealer WHERE ID=@ID",dealerID);
					tran.Commit();
					return dealerID;
				}
			}catch(Exception exc){ throw new ClientException(exc); }
		}

			#region Sub List
		[WebMethod]
		public string SubList(int dealerID)
		{
			return SQL.ADOToString(uox.dbADO,"SELECT * FROM t_Dealer_SalesPersons WHERE DealerID=?",dealerID);
		}
		[WebMethod]
		public void SubList_Delete(int salesID)
		{
			SQL.ExecuteNonQueryEx(uox.db,"DELETE FROM t_Dealer_SalesPersons WHERE ID=@ID",salesID);
		}
		[WebMethod]
		public void SubList_Update(int salesID,string fieldName,object fieldValue)
		{
			SQL.UpdateField(uox.db,"SELECT * FROM t_Dealer_SalesPersons WHERE ID="+salesID,fieldName,fieldValue);
		}
		[WebMethod]
		public int SubList_Add(int dealerID, XmlElement xmlContact){
			try{
				using( SqlConnection conn = new SqlConnection(uox.db) ){
					conn.Open();
					SqlTransaction tran = conn.BeginTransaction();
					int salesID = (int)SQL.ExecSP(tran,"s_DealerSalesPerson_Add",dealerID);
					SQL.UpdateDataTable(SQL.LoadDataTable(xmlContact),conn,tran,"SELECT * FROM t_Dealer_SalesPersons WHERE ID=@SalesID",salesID);
					tran.Commit();
					return salesID;
				}
			}catch(Exception exc) {	throw new ClientException(exc);	}
		}
			#endregion
	}
}
